<!doctype html>
<html lang="en" data-bs-theme="auto">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="description" content="">
  <title>添加栏目</title>

  <link href="/assets/dist/css/bootstrap.min.css" rel="stylesheet">
  <link rel="stylesheet" href="/assets/icons/font/bootstrap-icons.css">
  <link rel="stylesheet" href="/libs/tree/select-tree.css">
  
  <link href="/css/dashboard.css" rel="stylesheet">
  <script src="/js/jquery.min.js"></script>
  <script src="/libs/tree/select-tree.js"></script>
</head>

<body>

  <div class="container">
    <form class="row g-3 mt-1" id="form" method="post">
      <h3>添加栏目</h3>
      <div class="col-md-6">
        <label for="topicname" class="form-label">栏目名称</label>
        <input type="text" class="form-control" id="topicname" name="topicname">
      </div>
      <div class="col-md-6">
        <label for="inputEmail4" class="form-label">父栏目</label>
        <select class="form-select" id="parentid" name="parentid">
        </select>
      </div>

      <div class="col-md-4">
        <label for="topictype" class="form-label">栏目类型</label>

        <select id="topictype" class="form-select" name="topictype">
          <option  value="0">图文内容</option>
          <option  value="1">单页图文</option>
          <option  value="2">多页图文</option>
          <option  value="3">单页倒叙</option>
          <option  value="4">文档目录</option>
          <option  value="5">新闻列表</option>
          <option  value="6">新闻首页</option>
          <option  value="7">产品展示</option>
          <option  value="8">产品类别</option>
          <option  value="9">产品栏目</option>
          <option  value="10">产品列表</option>
          
          <!-- <option  value="11">商品列表</option>
          <option  value="12">商品展示</option>
          <option  value="13">博客类型</option>
          <option  value="14">兴趣论坛</option>
          <option  value="15">报名表单</option>
          <option  value="16">数据查询</option>
          <option  value="17">跳转地址</option>
          <option  value="18">菜单下拉</option>
          <option  value="19">菜单弹出层</option>
          <option  value="20">会员注册</option>
          <option  value="21">会员登录</option>
          <option  value="22">会员信息</option>
          <option  value="23">购物车</option> -->
          <option  value="24">品牌分类</option>
          <option  value="31">文章首页1</option>
          <option  value="32">文章首页2</option>
          <option  value="33">文章单页图片3</option>
          <option  value="34">文章列表无日期1</option>
          <option  value="35">文章单页图片4</option>
          <option  value="100">网站首页</option>
        </select>
      </div>
      <div class="col-md-2">
        <label for="topicstatus" class="form-label">状态</label>
        <div class="form-check form-switch mt-2">
          <input class="form-check-input" type="checkbox" role="switch" name="topicstatus" id="topicstatus" checked>
          <label class="form-check-label" for="topicstatus">是否显示</label>
        </div>

      </div>

      <div class="col-md-6">
        <label for="urlpath" class="form-label">栏目urlpath</label>
        <input type="text" class="form-control" id="urlpath" name="urlpath">
      </div>

      <div class="col-md-6">
        <label for="isside" class="form-label">是否有边栏</label>
        <div class="form-check form-switch mt-2">
          <input class="form-check-input" type="checkbox" role="switch" name="isside" id="isside" value="1">
          <label class="form-check-label" for="isside">是否边栏</label>
        </div>
      </div>
      <div class="col-md-6">
        <label for="sorttype" class="form-label">栏目内容排序</label>
        <select class="form-select" id="sorttype" name="sorttype">
            <option  value="0">最新在前面</option>
            <option  value="1">最新在后面</option>
            <option  value="2">排序值最小在前</option>
            <option  value="3">排序值最小在后</option>
        </select>
      </div>
      
      <div class="col-12">
        <label for="icoimg" class="col-sm-2 col-form-label text-sm-end">栏目图标</label>
        <div class="col-sm-6">
          <div class="input-group mb-3">
          <input type="text" class="form-control" id="icoimg" name="icoimg" value="" placeholder="可以留空，列表使用封面图或头图使用">
          <input class="form-control" type="file" style="width:100px" id="relate-icoimg" accept=".png,.jpg,.jpeg">
          </div>
        </div>
        <div class="col-sm-4">
          <div class="progress" id="progress-bar-show" style="display:none" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">
              <div class="progress-bar" id="progress-bar" style="width: 0%">0</div>
            </div>
          <div id="progress-bar-result"></div>  
        </div>
      </div> 

      <div class="col-12">
        <label for="memo" class="form-label">备注</label>
        <input type="text" class="form-control" id="memo" name="memo" value="">
      </div>

      <div class="col-12">
        <button type="submit" class="btn btn-primary">添加</button>
      </div>
    </form>

  </div>

  <div class="modal fade" id="myfiletobigModal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h1 class="modal-title fs-5" id="staticBackdropLabel">提示</h1>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body">
           文件太大了，请传4M以下的文件！
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
           
        </div>
      </div>
    </div>
  </div>

  <script src="/assets/dist/js/bootstrap.bundle.min.js"></script>
  <script lang="javascript">

    var topic_json = <% echo << obj["list"].to_json(); %> ;
    $('#parentid').append($('<option>').val("0").text("一级栏目").attr("selected", true));
    for (var i = 0; i < topic_json.length; i++) {
      if(topic_json[i].id==2)
      {
        $('#parentid').append($('<option>').val(topic_json[i].id).text(topic_json[i].value).data("pid", topic_json[i].parentid).attr("selected","selected"));
      }
      else
      {      
        $('#parentid').append($('<option>').val(topic_json[i].id).text(topic_json[i].value).data("pid", topic_json[i].parentid));
      }
   }

    var treeselect = $("#parentid").selectTree({ expandLevel: 1 });


    $("#form").on("submit", function (event) {
      event.preventDefault();

      var data = $(this).serialize();

      var obj = this;

      //得到序列化数据
      $.ajax({
        url: "/admin/addtopicpost",
        method: 'post',
        data: data,
        success: function (res) {
          //添加成功，这里演示
          console.log(res);
          if (res.code == 0) {
            topic_json = res.list;

            $('#parentid').empty();

            $('#parentid').append($('<option>').val("0").text("一级栏目").attr("selected", true));
            for (var i = 0; i < topic_json.length; i++) {
              $('#parentid').append($('<option>').val(topic_json[i].id).text(topic_json[i].value).data("pid", topic_json[i].parentid));
            }
            treeselect.clearSelect();
            // treeselect =$("#parentid").selectTree({ expandLevel: 1 });


          }
          $("#form").trigger("reset");
        }
      })
    });

    $(document).ready(function(e) {
        $("#relate-icoimg").change(function(){
            var fd=new FormData();
            if($(this).get(0).files[0].size>4596640){
                $('#myfiletobigModal').modal('show');
                return false;
            }
            fd.append("upfile",$(this).get(0).files[0]);
            $("#progress-bar-show").show();
            $("#progress-bar-result").hide();
            $.ajax({
                url:"/imageapi/upload?action=uploadimage",
                type:"POST",
                processData: false,
                contentType:false,
                data: fd,
                dataType: "json",
                xhr: function() {  // custom xhr
                    myXhr = $.ajaxSettings.xhr();
                    if(myXhr.upload){ // check if upload property exists
                        myXhr.upload.addEventListener('progress',function(evt){
                            var percentComplete = Math.round(evt.loaded*100 / evt.total);
                            $( "#progress-bar").css("width",percentComplete*2);
                            $( "#progress-bar").html(percentComplete+'%');
                        }, false); // for handling the progress of the upload
                    }
                    return myXhr;
                },
                success:function(data){
                    if(data.state=="SUCCESS"){
                        $("#progress-bar-show").hide();
                        $("#progress-bar-result").show();
                        $("#icoimg").val(data.url);
						$("#progress-bar-result").html("<a href='"+data.url+"' target=_blank><img height=\"50\" width=\"50\"  src=\""+data.url+"\"/></a>");
                       $('#myfilesuccessModal').modal('show');
                    }
                     
                }
            });
        });
    });	  
  </script>
</body>

</html>
